<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 查询面板 -->
      <div class="card-header">
        <div class="search-title">
          <i class="el-icon-search"></i>
          <span style="margin-left:10px">筛选查询</span>
        </div>
        <el-button type="primary" size="small" @click="handleOrderQuery">查询结果</el-button>
      </div>
      <el-form inline :model="queryForm" label-width="150px">
        <el-form-item label="支付状态">
          <el-select v-model="queryForm.pay_status" placeholder="全部" clearable>
            <el-option label="未付款" value="0"></el-option>
            <el-option label="已付款" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否发货">
          <el-select v-model="queryForm.is_send" placeholder="全部" clearable>
            <el-option label="未发货" value="0"></el-option>
            <el-option label="已发货" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top:20px">
      <!-- 订单列表 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="订单编号" prop="order_number" min-width="100"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="100"></el-table-column>
        <el-table-column label="是否付款" width="110">
          <template v-slot="scope">
            <el-tag v-if="scope.row.order_pay==='0'" type="warning">
              未付款
            </el-tag>
            <el-tag v-else type="success">
              已付款
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="100"></el-table-column>
        <el-table-column label="下单时间" width="170">
          <template v-slot="scope" >
            {{scope.row.create_time|formatDateTime}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <el-button icon="el-icon-edit" type="primary" size="mini"
            @click="addrDialogVisable = true"
          >编辑</el-button>
          <el-button icon="el-icon-location" type="success" size="mini"
            @click="handleLogisticsGet"
          >物流</el-button>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentPageChange"
      :current-page="queryForm.pagenum"
      :page-sizes="[5, 10, 15, 20, 25]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 修改地址 -->
    <el-dialog title="修改地址" :visible.sync="addrDialogVisable" @close="handleAddrDialClose">
      <el-form :model="addrForm" ref="addrFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="addrRegion" required>
          <el-cascader
            :options="regionData"
            v-model="addrForm.addrRegion"
            @change="handleRegionChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="addrDetail" required>
          <el-input v-model="addrForm.addrDetail" placeholder="请输入详细地址"></el-input>
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-button @click="addrDialogVisable = false">取 消</el-button>
        <el-button type="primary" @click="addrDialogVisable = false">确 定</el-button>
      </template>
    </el-dialog>
    <!-- 查看物流 -->
    <el-dialog class="logistics-dialog" title="物流进度" :visible.sync="logisticsDialogVisable" >
      <el-timeline>
        <el-timeline-item v-for="logi in logisticsInfo" 
          :key="logi.time"
          :timestamp="logi.time"
          :icon="logi.icon"
          :color="logi.color"
        >
          {{logi.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import { regionData } from 'element-china-area-data'
import { 
  getOrdersByPage,
  getLogisticsById
} from '../../api/orders'
export default {
  created(){
    this.getOrderList()
  },
  data(){
    return {
      total:0,
      queryForm:{
        pagesize:5,
        pagenum:1,
        pay_status:'',
        is_send:''
      },
      orderList:[],
      // 地址弹框
      addrDialogVisable:false,
      // 地址数据
      regionData,
      // 地址表单
      addrForm:{
        addrRegion:[],
        addrDetail:''
      },
      // 物流弹框
      logisticsDialogVisable:false,
      // 物流信息
      logisticsInfo:[
        {
          "time": "2018-05-10 09:39:00",
          "ftime": "2018-05-10 09:39:00",
          "context": "已签收,感谢使用顺丰,期待再次为您服务",
          "location": "",
        },
        {
          "time": "2018-05-10 08:23:00",
          "ftime": "2018-05-10 08:23:00",
          "context": "[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件",
          "location": ""
        },
        {
          "time": "2018-05-10 07:32:00",
          "ftime": "2018-05-10 07:32:00",
          "context": "快件到达 [北京海淀育新小区营业点]",
          "location": ""
        },
        {
          "time": "2018-05-10 02:03:00",
          "ftime": "2018-05-10 02:03:00",
          "context": "快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]",
          "location": ""
        },
        {
          "time": "2018-05-09 23:05:00",
          "ftime": "2018-05-09 23:05:00",
          "context": "快件到达 [北京顺义集散中心]",
          "location": ""
        },
        {
          "time": "2018-05-09 21:21:00",
          "ftime": "2018-05-09 21:21:00",
          "context": "快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]",
          "location": ""
        },
        {
          "time": "2018-05-09 13:07:00",
          "ftime": "2018-05-09 13:07:00",
          "context": "顺丰速运 已收取快件",
          "location": ""
        },
        {
          "time": "2018-05-09 12:25:03",
          "ftime": "2018-05-09 12:25:03",
          "context": "卖家发货",
          "location": ""
        },
        {
          "time": "2018-05-09 12:22:24",
          "ftime": "2018-05-09 12:22:24",
          "context": "您的订单将由HLA（北京海淀区清河中街店）门店安排发货。",
          "location": ""
        },
        {
          "time": "2018-05-08 21:36:04",
          "ftime": "2018-05-08 21:36:04",
          "context": "商品已经下单",
          "location": ""
        }
      ]
      
    }
  },
  methods:{
    async getOrderList(){
      try {
        const res = await getOrdersByPage(this.queryForm)
        // console.log(res.data);
        this.total = res.data.total
        this.orderList = res.data.goods
      } catch (err) {
        console.log(err);
      }
    },
    // 筛选查询
    handleOrderQuery(){
      this.queryForm.pagenum = 1
      this.getOrderList()
    },
    // 分页控制
    handleSizeChange(pagesize) {
      this.queryForm.pagenum = 1
      this.queryForm.pagesize = pagesize
      this.getOrderList()
    },
    handleCurrentPageChange(pagenum) {
      this.queryForm.pagenum = pagenum
      this.getOrderList()
    },

    // 区域地址改变
    handleRegionChange(v){
      console.log(v);
    },
    handleAddrDialClose(){
      this.$refs.addrFormRef.resetFields()
    },
    // 查看物流
    handleLogisticsGet(){
      this.logisticsDialogVisable = true
      //展示Mock数据
      this.logisticsInfo = this.logisticsInfo.map((item,i)=>{
       if(i === 0){
          return {
            ...item,
            'color': '#0bbd87',
            'icon': 'el-icon-circle-check'
          }
        }else{
          return {
            ...item,
            'color':'',
            'icon': ''
          }
        }
      })
      
      
    }
  }
}
</script>
<style scoped>
.card-header{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.search-title{
  display: inline-block;
}
.el-cascader{
  width: 100%;
}
.logistics-dialog >>> .el-dialog__body{
  max-height: 340px;
  overflow: auto;
}
</style>